﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using BankSystem.Classes;

namespace BankSystem.Forms
{
    public partial class ParentForm : Form
    {
        private MainForm mainForm;
        private LoginForm loginForm;
        private WithdrawForm withdrawForm;
        private StoreForm storeForm;
        private BalanceForm balanceForm;

        List<Bank> BankList = new List<Bank>();

        Account accountDelt=null;
        ATM atmDelt=null;

        public ParentForm()
        {
            InitializeComponent();
            mainForm = new MainForm(this);
            loginForm = new LoginForm(this);
            withdrawForm = new WithdrawForm(this);
            storeForm = new StoreForm(this);
            balanceForm = new BalanceForm(this);

            loginForm.Show();
            init();
        }

        private void init()
        {
            BankList.Add(new Bank("BANK_A"));
            BankList.Add(new Bank("BANK_B"));
            BankList.Add(new Bank("BANK_C"));

            foreach (Bank bank in BankList)
            {
                for(int i = 0;i<4;i++)
                {
                    ATM atm = bank.AddATM();
                    atm.NoticeBigMoney += dealBigMoney;
                }
                for(int i = 0;i<4; i++)
                {
                    bank.AddAccount("1111");
                }
            }

            List<string> atmInfo = new List<string>();
            foreach (Bank bank in BankList)
            {
                foreach(ATM atm in bank.ATMs)
                {
                    atmInfo.Add(atm.getID());
                }
            }

            loginForm.init(atmInfo);
        }

        public void jumpToMainForm(Form form) { form.Hide(); mainForm.Show(); }
        public void jumpToLoginForm(Form form) { form.Hide(); loginForm.Show(); }
        public void jumpToWithdrawForm(Form form) { form.Hide(); withdrawForm.Show(); }
        public void jumpToStoreForm(Form form) { form.Hide(); storeForm.Show(); }
        public void jumpToBalanceForm(Form form) { form.Hide(); balanceForm.updateText(accountDelt); balanceForm.Show(); }


        /// <summary>
        /// 登录，遍历所有银行的所有账户，若账号密码匹配则返回true
        /// </summary>
        /// <param name="id"></param>
        /// <param name="password"></param>
        /// <param name="atm"></param>
        /// <returns></returns>
        public bool login(string id,string password,string atm)
        {
            foreach(Bank bank in BankList)
            {
                foreach(ATM Atm in bank.ATMs)
                {
                    string searchATM=Atm.getID();
                    if (searchATM.Equals(atm))
                    {
                        atmDelt = Atm;
                        mainForm.setATMid(Atm.getID());
                        goto z;
                    }
                }
            }
            z:
            foreach (Bank bank in BankList)
            {
                foreach(Account account in bank.Accounts)
                {
                    string searchId = account.getID();
                    if (id.Equals(searchId))
                        if ((account.CheckPassword(password)))
                        {
                            accountDelt=account;
                            return true;
                        }
                }
            }
            return false;
        }

        /// <summary>
        /// 存钱，可能抛出AccountDealingException异常
        /// </summary>
        /// <param name="amount"></param>
        /// <param name="infoATM"></param>
        public void storeMoney(double amount, out string infoATM)
        {
            double addAmount = atmDelt.saveMoney(accountDelt, amount, out infoATM);
            accountDelt.SaveMoney(addAmount);
        }

        /// <summary>
        /// 取钱，可能抛出AccountDealingException异常
        /// </summary>
        /// <param name="amount"></param>
        /// <param name="infoATM"></param>
        public void withdrawMoney(double amount, out string infoATM)
        {
            double cost = atmDelt.withdrawMoney(accountDelt, amount, out infoATM);
            accountDelt.WithdrawMoney(cost);
        }

        /// <summary>
        /// 退出该账户，清空相关信息
        /// </summary>
        public void Exit()
        {
            this.accountDelt = null;
            this.atmDelt = null;
            loginForm.init();
        }

        /// <summary>
        /// 处理金额过大事件
        /// </summary>
        /// <param name="storeOrWithdraw"></param>
        /// <param name="amount"></param>
        public void dealBigMoney(bool storeOrWithdraw, double amount)
        {
            if(storeOrWithdraw)
            {
                withdrawForm.noticeBigMoney(amount);
            }
            else
            {
                storeForm.noticeBigMoney(amount);
            }
        }
    }
}
